﻿#region

using System;
using System.Collections.Generic;
using MediaSage.Lib.Configuration;
using MediaSage.Lib.Utilities;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace MediaSage.Lib.Database
{
    public class MediaDatabase : Database
    {
        #region Constructors

        public MediaDatabase(Config Config)
            : base(Config)
        {
            logger.Log("CVideoDatabase Initalized");
        }

        #endregion Constructors

        #region Methods

        public IList<MediaClass> GetMedia(string WhereClause)
        {
            logger.Log("Entering GetMedia(" + WhereClause + ")");
            var ReturnValue = new List<MediaClass>();

            IList<VideoEntity> data =
                DBUtil.GetSession().CreateCriteria(typeof(VideoEntity)).Add(Expression.Sql(WhereClause)).List
                    <VideoEntity>();
            foreach (VideoEntity entity in data)
            {
                var mediaClass = new MediaClass(config);
                mediaClass.videoEntity = entity;
                ReturnValue.Add(mediaClass);
            }

            logger.Log("Exiting GetMedia(" + WhereClause + "), with " + ReturnValue.Count + " results");
            return ReturnValue;
        }

        public IList<MediaClass> GetMedia(string WhereClause, int Limit)
        {
            logger.Log("Entering GetMedia(" + WhereClause + "," + Limit + ")");
            var ReturnValue = new List<MediaClass>();

            IList<VideoEntity> data =
                DBUtil.GetSession().CreateCriteria(typeof(VideoEntity)).Add(Expression.Sql(WhereClause)).SetMaxResults(
                    Limit).List<VideoEntity>();
            foreach (VideoEntity entity in data)
            {
                var mediaClass = new MediaClass(config);
                mediaClass.videoEntity = entity;
                ReturnValue.Add(mediaClass);
            }

            logger.Log("Exiting GetMedia(" + WhereClause + "," + Limit + "), with " + ReturnValue.Count + " results");
            return ReturnValue;
        }

        ///<summary>
        ///    Save a mediaObject to the database
        ///    This needs to check for unique items like Images (Unique by their SHA Checksum, if they already exist in the database
        ///    and in the image folder, the object should be changed to link to that and not resave them
        ///</summary>
        ///<param name="mediaObject"> </param>
        ///<returns> </returns>
        public bool SaveToDatabase(VideoEntity mediaObject)
        {
            logger.Log("Entering CMediaDatabase.SaveToDatabase()");
            if (mediaObject == null)
            {
                logger.Log(Logger.Levels.ERROR,
                           "SaveToDatabase called with a null VideoEntity, this is an error condition.");
                logger.Log("Exting SaveToDatabase(), rc==false");
                return (false);
            }
            mediaObject.LastModified = DateTime.Now;
            ITransaction transaction = null;
            try
            {
                transaction = DBUtil.GetSession().BeginTransaction();
                DBUtil.GetSession().Save(mediaObject);
                transaction.Commit();
                transaction.Dispose();
                DBUtil.GetSession().Flush();
            }
            catch (Exception E)
            {
                config.Logger.Log(Logger.Levels.ERROR,
                                  "Exception caught storing videoEntity, Exception Message: " + E.Message);
                if (transaction != null)
                {
                    transaction.Dispose();
                }
                DBUtil.GetSession().Close();
                config.Logger.Log("Exiting CMediaDatabase.SaveToDatabase(), rc==false");
                return false;
            }
            config.Logger.Log("Exiting CMediaDatabase.SaveToDatabase(), rc==true");
            return true;
        }

        #endregion Methods
    }
}